package cuit.cx.groupcomment.service;

import java.util.List;

import cuit.cx.groupcomment.dao.UserinfoDAO;
import cuit.cx.groupcomment.model.Userinfo;

public class UserinfoService 
{
	private UserinfoDAO userinfoDAO;

	public UserinfoDAO getUserinfoDAO() 
	{
		return userinfoDAO;
	}

	public void setUserinfoDAO(UserinfoDAO userinfoDAO) 
	{
		this.userinfoDAO = userinfoDAO;
	}

	public Userinfo register(String email, String name, String phoneNumber, String password, String power)
	{
		
		Userinfo userinfo = new Userinfo();	
		//检查用户是否已注册
		boolean flag = userinfoDAO.checkUserIsExist(email);
		if (!flag)
		{
			userinfo.setName(name);
			userinfo.setEmail(email);
			userinfo.setPassword(password);
			userinfo.setPhoneNumber(phoneNumber);
			userinfo.setCreateTime(System.currentTimeMillis());
			userinfo.setPower(power);
			userinfoDAO.insert(userinfo);
			userinfo = userinfoDAO.findUserinfo(userinfo);
		}
		else
		{
			userinfo = null;
		}		
		return userinfo;		
	}
	
	public Userinfo login(String userEmail, String userPassword)
	{
		
		Userinfo userinfo = new Userinfo();
		userinfo.setEmail(userEmail);
		userinfo.setPassword(userPassword);
		List<Userinfo> userinfos = userinfoDAO.getUserinfos(userinfo);
		if (userinfos != null && userinfos.size() == 1)
		{
			return userinfos.get(0);
		}		
		return null;
	}
	
	public Userinfo changeUserinfo(String id, String email, String name, String phoneNumber, String password)
	{
		Userinfo userinfo = new Userinfo();
		userinfo.setId(Integer.parseInt(id));
		userinfo.setEmail(email);
		userinfo.setName(name);
		userinfo.setPhoneNumber(phoneNumber);
		userinfo.setPassword(password);
		int result = userinfoDAO.updateUserinfo(userinfo);
		if (result > 0)
		{
			userinfo = userinfoDAO.findUserinfoById(Integer.parseInt(id));
		}
		else
		{
			userinfo = null;
		}
		return userinfo;
	}
	
	public List<Userinfo> findUserinfos() 
	{
		return userinfoDAO.getUserinfos(null);
	}
	
	
	
	

}
